feat: 支持按模型配置 token 价格,聚合 API 模型同步时自动创建零价格规则#279
Open
panzeyu2013 wants to merge 1 commit into
Open
Conversation
…egate API models
- Auto-create model_price_rules (price=0) when syncing aggregate API models
- Only insert on first sync, never overwrite existing user-set prices
- Add RPC endpoints: quota/modelPriceRules/list, read, upsert
- Add price input fields (input/cached/output per 1M tokens) to model edit modal
- Register Tauri commands for desktop support
- User-created rules use priority=20000,ID=user-{slug} to override official seeds
KilimiaoSix
requested changes
May 27, 2026
Collaborator
KilimiaoSix
left a comment
There was a problem hiding this comment.
Request changes,暂不建议合并。
这次改动方向是合理的,但当前实现里有几个会影响功能正确性的点,需要先修复:
-
model-catalog-modal.tsx里的价格字段打开弹窗时始终是空值,编辑已有模型时不会读取已有价格规则。用户只改一个价格字段时,其他未填字段会被保存成null,而后端计价逻辑要求 input/output 价格都存在,可能导致整条自定义价格规则失效。建议保存前先读取并预填现有规则,更新时做 merge,而不是把空字段覆盖为null。 -
聚合 API 自动创建的零价格规则优先级是
-10,但官方价格 seed 的优先级是10000左右,实际匹配会优先命中官方规则。因此普通 OpenAI 模型名不会按自动创建的零价格规则计费,这和 PR 描述不一致。建议明确自动零价规则的优先级策略,或者只在没有更高优先级用户规则时生成能够实际生效的规则。 -
价格规则保存失败目前被前端吞掉了,弹窗仍然关闭,用户会误以为模型和价格都保存成功。建议价格保存失败时展示错误并阻止关闭,或者把模型保存和价格保存设计成清晰的两阶段状态。
另外,当前 diff 还有 trailing whitespace,git diff --check 会失败,需要清理。
验证情况:
cargo check -p codexmanager-service通过cargo test -p codexmanager-service aggregate_通过git diff --check 158adf8...HEAD失败,存在 trailing whitespacepnpm -C apps run build未能执行,临时 worktree 缺少apps/node_modules,找不到next
Contributor
Author
|
合理的,我再重点排查一下和目前定价相关的逻辑 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更摘要
问题:聚合 API(如 DeepSeek、Mistral)的模型同步后缺少
model_price_rules记录,导致定价引擎匹配失败,estimated_cost_usd始终为null(前端显示price_status: "missing"),钱包不扣费,成本统计无法正常工作。根因:
auto_associate_source_models(crates/service/src/apikey/apikey_models.rs:821)在同步聚合 API 模型时只创建了 platform model 目录条目和model_source_mapping路由映射,没有创建价格规则。定价引擎estimate_cost_usd_for_log先查 DB rules → 未命中 → fallback 硬编码PRICE_SEEDS→ 也未命中 → 返回None → 0.0。方案:同步时自动为聚合 API 模型创建价格规则(价格均为 0),并支持用户在模型编辑弹窗中手动设置价格。用户规则
priority=20000,确定性 ID=user-{slug},确保覆盖所有自动创建和官方 seed 规则。改动范围
主要文件
Rust 后端(4 个文件)
crates/core/src/rpc/types.rsModelPriceRuleEntryModelPriceRuleListResultModelPriceRuleUpsertInputcrates/service/src/quota/read.rslist_model_price_rulesread_model_price_ruleupsert_model_price_ruleprice_rule_entry辅助函数crates/service/src/rpc_dispatch/quota.rsquota/modelPriceRules/listquota/modelPriceRule/readquota/modelPriceRule/upsertcrates/service/src/apikey/apikey_models.rsensure_model_price_rules_for_aggregate_apiexisting_patternsHashSet先查后插for source_model in source_models的 borrow-after-move 编译错误前端(5 个文件)
apps/src/lib/api/account-client.tsModelPriceRuleEntryModelPriceRuleUpsertPayloadlistModelPriceRulesreadModelPriceRuleupsertModelPriceRuleapps/src/lib/api/transport-web-commands.tsservice_model_price_rules_listservice_model_price_rule_readservice_model_price_rule_upsertapps/src/components/modals/model-catalog-modal.tsxonSavePriceRule回调同步写入规则apps/src/hooks/useManagedModels.tssaveModelPriceRulehook 方法apps/src/app/models/page.tsxonSavePriceRule桌面端 Tauri(2 个文件)
apps/src-tauri/src/commands/apikey.rsservice_model_price_rules_listservice_model_price_rule_readservice_model_price_rule_upsert#[tauri::command]apps/src-tauri/src/commands/registry.rsinvoke_handler!宏中注册上述三个命令验证
pnpm -C apps run testpnpm -C apps run buildpnpm -C apps run test:uicargo test --workspace已执行的实际验证:
Rust 编译检查
cargo check --workspace
结果:0 errors, 0 warnings (0.82s)
Rust 全部测试
cargo test --workspace
结果:1122 tests passed, 0 failed, 0 ignored
前端构建
pnpm -C apps run build
结果:12 static pages, 0 errors
桌面端构建(含 Tauri)
pnpm -C apps run build:desktop
结果:12 static pages, Tauri commands registered, 0 errors
风险与影响面
直接影响
定价引擎:
model_pricing.rs不变)请求日志:
request_log.rs、aggregate_api.rs、proxy.rs等均不变)OpenAI account 同步:
source_kind == "aggregate_api"触发)官方模型定价:
priority=20000priority=-10priority≈999920000 > 9999 > -10边界注意
修复前:
cost=0修复后:
cost仍为 0base_cost_usd <= 0.0跳过)重同步保护:
existing_patterns包含所有已启用规则的model_pattern编辑已有模型:
备注
自动创建的 rule 标识:
source: "aggregate_api_sync"priority=-10官方 seed:
source: "official_seed"用户规则:
source: "custom"priority=20000分层关系清晰
用户 upsert 使用 ID:
user-{model_pattern}特性: